package com.sniff.atp.controller;

import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.constant.LoginConstant;
import com.sniff.atp.entity.ToolsInfo;
import com.sniff.atp.manager.ToolsInfoMananager;
import com.sniff.atp.modules.test.service.impl.CustomerGuideService;
import com.sniff.atp.response.SniffResponse;
import com.sniff.atp.service.common.GetTestPlanFromAliyun;
import com.sniff.atp.service.order.PurchaseAllotOrder;
import com.sniff.atp.service.purchase.PurchaseService;
import com.sniff.atp.utils.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.jar.JarEntry;

@Api(value = "工具类", tags = "工具类")
@RestController
@RequestMapping("/tool")
public class ToolController {
    @Autowired
    private PurchaseAllotOrder purchaseAllotOrder;
    @Autowired
    private ToolsInfoMananager toolsInfoMananager;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private GetTestPlanFromAliyun getTestPlanFromAliyun;

    @Autowired
    private CustomerGuideService customerGuideService;


    @ApiOperation("根据订单号，一键分单")
    @GetMapping("/allot")
    public Object allot(@RequestParam String platformOrderNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(24L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        //调用接口进行分单
        purchaseService.allotAutoOrder(LoginConstant.systemLoginDTO, platformOrderNo);
        //xxljob停用，后续使用powerjob
        //purchaseAllotOrder.allotAutoOrder();
        String result = purchaseAllotOrder.getPurchaseOrderNo(platformOrderNo);
        return SniffResponse.pass(JsonUtil.strToJSONObject(result));
    }

    @ApiOperation("爬虫任务")
    @GetMapping("/testPlan")
    public Object getTestPlan(@RequestParam String cookies) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(33L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        String msg = "任务已经在执行中，请耐心等待；大约5h后执行完成";

        getTestPlanFromAliyun.getTestPlan(cookies);

        return SniffResponse.pass(msg);
    }

    @ApiOperation("新增一条工具调用记录")
    @GetMapping("/addToolInfoRecord")
    public Object addToolInfoRecord(@RequestParam Long toolInfoId) {
        //表：tools_info
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(toolInfoId);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        String msg = "添加成功";
        return SniffResponse.pass(msg);
    }
    @ApiOperation("测试数据库连接")
    @GetMapping("test")
    @ResponseBody
    public Object customerGuideService(){
        return customerGuideService.getAll(1L);
    }

    @ApiOperation("测试数据库连接")
    @GetMapping("test1")
    @ResponseBody
    public Object customerGuideService1(){
        return customerGuideService.getAllList();
    }

    @ApiOperation("测试数据库连接")
    @GetMapping("test2")
    @ResponseBody
    public Object customerGuideService2(){
        return customerGuideService.getAllList1();
    }
}
